layui.config({
    base : "/js/common/"
}).use(['form','layer','table','laypage','jquery','common'],function() {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laypage=layui.laypage,
        common=layui.common,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        url: 'findList',
        cellMinWidth: 95,
        page: true,
        limits: [10, 15, 20, 25],
        limit: 20,
        id: "userListTable",
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: 'userName', title: '登录名称', minWidth: 100, align: "center"},
            {field: 'realName', title: '姓名', minWidth: 100, align: "center"},
            {field: 'email', title: '用户邮箱', minWidth:200, align:'center'},
            {field: 'sex', title: '用户性别', align:'center'},
            {field: 'age', title: '年龄', align:'center'},
            {field: 'birthday', title: '生日', align:'center',templet:'<div>{{layui.laytpl.toDateString(d.birthday)}}</div>',unresize: true},
            {field: 'locked', title: '用户状态',  align:'center',templet:'#user-state'},
            // {field: 'userEndTime', title: '最后登录时间', align:'center',minWidth:150},
            {title: '操作', minWidth: 175, templet: '#userListBar', fixed: "right", align: "center"}
        ]],
        done: function(res, curr, count){
            //pages=res.page;
            counts=res.count;
            //完整功能
            laypage.render({
                elem: 'divPage'
                ,count: counts,
                first: '首页',
                last: '尾页'
                ,layout: ['count', 'prev', 'page', 'next',  'refresh', 'skip']
                ,theme: '#00A0E9'
                ,curr: curr
                ,skip: true
                ,jump: function(obj,first){
                    curr = obj.curr;

                    if(!first){
                        getData(obj.curr,obj.limit)
                    }
                }
            });

        }
    });
    function getData(page,limit){
        $.ajax({
            type: 'get',
            url: 'findList?page='+page+"&limit="+limit,
            async: false,
            success: function (data) {
                $(".layui-laypage-btn").click();
                table.reload('userListTable',{
                    data : data,
                    page:{
                        curr:page
                    }
                });

            }
        });
    }
    //搜索
    form.on("submit(searchForm)",function(data){
        table.reload('userListTable', {where:data.field});
    });
    //添加用户
    $(".addUser_btn").click(function () {
        var url = "addUI";
        common.cmsLayOpen('添加用户', url, '1080px', '700px');
    })

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data;
        if(data.length > 0){
            console.log(JSON.stringify(data));
            for(var i=0;i<data.length;i++){
                var d = data[i];
                if(d.id === 1){
                    layer.msg("不能删除超级管理员");
                    return false;
                }
            }
            layer.confirm("你确定要删除这些用户么？",{btn:['是的,我确定','我再想想']},
                function(){
                    var deleteindex = layer.msg('删除中，请稍候',{icon: 16,time:false,shade:0.8});
                    $.ajax({
                        type:"POST",
                        url:"batchDelete",
                        dataType:"json",
                        contentType:"application/json",
                        data:JSON.stringify(data),
                        success:function(res){
                            layer.close(deleteindex);
                            if(res.success){
                                layer.msg("删除成功",{time: 1000},function(){
                                    table.reload('userListTable',{where:data.field});
                                });
                            }else{
                                layer.msg(res.message);
                            }
                        }
                    });
                }
            )
        }else{
            layer.msg("请选择需要删除的用户",{time:1000});
        }
    })

    //列表操作
    table.on('tool(userList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') { //编辑
            var url = "editUI?id="+data.id;
            common.cmsLayOpen('修改用户', url, '1080px', '700px');
        }else if (layEvent === 'del') { //删除
            layer.confirm('确定删除此用户？', {icon: 3, title: '提示信息'},
                function(){
                    $.post("delete",{"id":data.id},function (res){
                        if(res.success){
                            layer.msg("删除成功",{time: 1000},function(){
                                location.reload();
                            });
                        }else{
                            layer.msg(res.message);
                        }
                    });
            });
        }
    })
    // 修改user状态
    form.on('switch(user-state)', function (obj) {
        if (obj.elem.value== null) {
            layer.msg("用户ID不存在");
            return false;
        }
        var loadIndex = layer.load(2, {
            shade: [0.3, '#333']
        });
        $.post("updateUserState",{
            id: obj.elem.value,
            locked: obj.elem.checked ? 0 : 1
        }, function (res) {
            layer.close(loadIndex);
            if (res.success) {
                parent.layer.msg(res.message, {time: 1500}, function () {
                  location.reload();
                });
            } else {
                layer.msg(res.message);
            }
        });
        return false;

    });
});
